import 'package:flutter/material.dart';
import 'package:dart_sdk/channel_state.dart';

class ChannelList extends StatefulWidget {
  const ChannelList(
      {super.key,
      required this.channels,
      required this.selectedChannel,
      required this.onSelectedChannelChanged});
  final List<ChannelState> channels;
  final String selectedChannel;
  final ValueChanged<String> onSelectedChannelChanged;
  @override
  State<ChannelList> createState() => _ChannelListState();
}

class _ChannelListState extends State<ChannelList> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemBuilder: (itemBuilder, index) {
          return AnimatedContainer(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(color: Colors.grey.withOpacity(0.5)))),
            duration: const Duration(seconds: 1),
            child: ListTile(
              selected:
                  widget.selectedChannel == widget.channels[index].channel,
              title: Text(widget.channels[index].channel ?? ""),
              subtitle: Text(widget.channels[index].data ?? ""),
              trailing: Text(widget.channels[index].timestamp == null
                  ? ""
                  : widget.channels[index].timestamp.toString()),
              dense: true,
              onTap: () => widget.onSelectedChannelChanged(
                  widget.channels[index].channel ?? ""),
            ),
          );
        },
        itemCount: widget.channels.length);
  }
}
